// index.js
import { Toast } from 'tdesign-miniprogram';


const appInstance = getApp()
const imageCdn = 'https://tdesign.gtimg.com/mobile/demos';
const swiperList = [];

Component({
  properties: {
    scrollTop: { type: Number, value: 0},
  },
  data: {
    confirmBtn: { content: '知道了', variant: 'base' },
    open_dialog:false,
    notice_content:null,
    notice_title:null,
    display_login_page:false,
    footer_text: '对小程序不满意? 请点击上方按钮进行反馈😬',
    links: [
        {
          url: '/pages/index/index',
          openType: 'switchTab',
        }
    ],
    backTopTheme: 'half-round-dark',
    backTopText: '客服',
    current: 1,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList,
    navigation: { type: 'dots-bar' },
    paginationPosition: 'right',
    value: '',
    actionText: '',
    phone_number:'',
    verify_code:'',
    is_manager:false,
    text: null,
    links: [
      [
        {
          name: '管理员登录🧔‍♂️',
          url: "/pages/web_view_page/index?manager_login_model=true",
          openType: 'navigate',
        },
      ],
    ],
  },

  methods: {
    closeDialog() {
      this.setData({
        open_dialog:false
      })
    },
    // 登陆相关
    InputPhoneNumber(e){
      this.setData({phone_number:e.detail.value})
    },
    InputVerifyCode(e){
      this.setData({verify_code:e.detail.value})
    },
    fail_toast(message){
      wx.showToast({
        title: message,
        icon: 'none',
        duration: 2000
      })
    },
    success_toast(message){
      wx.showToast({
        title: message,
        icon: 'success',
        duration: 2000
      })
    },
    register(e){
      let data = {
        'phone_number':this.data.phone_number,
        'verify_code':this.data.verify_code,
      }
      let method = 'POST'
      let url = appInstance.globalData.request_url + 'register'
      let that = this
      wx.login({
        success: (res) => {
          wx.request({
            url:url,
            data:Object.assign({wx_code:res.code},data),
            method:method,
            success(resp){
              let msg = resp.data.result.msg
              let status = resp.data.result.status
              if (status == 200){
                that.setData({
                  display_login_page:false
                })
                that.success_toast(msg)
              }else{
                that.fail_toast(msg)
              }
            },
            fail(err){
              that.fail_toast(err)
            }
          })
        },
      })
    },
    action_notice_content(e){
      console.log(e)
      let id = e.currentTarget.dataset.id
      let index_notice_list = this.data.index_notice_list
      index_notice_list.forEach((item) => {
        if (item.id == id){
            this.setData(
              {
                open_dialog:true,
                notice_content:item.content,
                notice_title:item.title
              }
            )
        }
      })
    },
    onLoad(){
      let data = {
      }
      let method = 'POST'
      let url = appInstance.globalData.request_url + 'login'
      let that = this
      wx.login({
        success: (res) => {
          wx.request({
            url:url,
            data:Object.assign({wx_code:res.code},data),
            method:method,
            success(resp){
              console.log(resp.data.result.status)
              console.log(resp.data.result)
              if (resp.data.result.status != 200){
                that.setData({
                  display_login_page:true
                })
              }
              that.setData(
                {
                  swiperList:resp.data.result.index_image_list,
                  index_notice_list:resp.data.result.index_notice_list,
                  contact_phone:resp.data.result.contact_phone,
                  is_manager:resp.data.result.is_manager
                }
              )
            },
            fail(err){
              wx.showToast({
                title: err,
                icon: 'none',
                duration: 2000
              })
          
            }
          })
        },
      })

    },

    onClose(){
      this.setData({
        display_login_page:false
      })
    },
    handleBack() {
      console.log('go back');
    },

    //客服
    onToTop(e) {
      let contact_phone = this.data.contact_phone
      wx.makePhoneCall({
        phoneNumber: contact_phone //仅为示例，并非真实的电话号码
      })
    },
    // 轮播图相关
    onChange(e) {
      const {
        detail: { current, source },
      } = e;
      console.log(current, source);
    },
    onAutoplayChange(e) {
      this.setData({
        autoplay: e.detail.value,
      });
    },
    onIntervalChange(e) {
      this.setData({
        interval: e.detail.value,
      });
    },
    onDurationChange(e) {
      this.setData({
        duration: e.detail.value,
      });
    },

// 搜索相关
    changeHandle(e) {
      const { value } = e.detail;
      // console.log(value)
      this.setData({
        search_content:value
      });
    },

    focusHandle() {
      this.setData({
        actionText: '搜索',
      });
    },

    blurHandle() {
      this.setData({
        actionText: '',
      });
    },

    actionHandle() {
      console.log('触发')
      let search_content = this.data.search_content
      wx.navigateTo({
        url: `/pages/search_content/index?search_content=${search_content}`,
      })

    },
  },
});
